# /usr/bin/env python3.6
# -*- encoding: utf-8 -*-
"""
@File    : city_api.py
@Time    : 2020/2/6 0006 17:20
@Author  : zhangbing
@Email   : 18829172841@163.com
@Software: PyCharm
"""
from flask_restful import Resource, fields, marshal

from App.apis.api_constant import HTTP_OK
from App.models.common.city_model import Letter, City

'''
此接口主要用于处理相关城市信息查询
'''
city_feilds = {
    "id": fields.Integer(attribute="c_id"),
    "parentId": fields.Integer(attribute="c_parent_id"),
    "regionName": fields.String(attribute="c_region_name"),
    "cityCode": fields.Integer(attribute="c_city_code"),
    "pinYin": fields.String(attribute="c_pinyin"),
}


class CitiesResource(Resource):
    def get(self):
        letters = Letter.query.all()
        letter_cities = {}
        letter_cities_fields = {}
        for letter in letters:
            letter_citie = City.query.filter_by(letter_id=letter.id)
            letter_cities[letter.letter] = letter_citie
            letter_cities_fields[letter.letter] = fields.List(fields.Nested(city_feilds))
        return {
            "msg": "ok",
            "status": HTTP_OK,
            "data": marshal(letter_cities, letter_cities_fields)
        }


if __name__ == "__main__":
    pass
